package f.h.dragon.test.asm;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import static org.objectweb.asm.Opcodes.*;

import static f.h.dragon.util.OutUtil.*;

/**
 * GenerateClassTester: generate class 
 *
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2012/04/01
 * @since 1.0
 */
public class GenerateClassTester {

	/**
	 * test... 
	 * 
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception{
		ClassWriter cw = new ClassWriter(0);
		cw.visit(V1_5, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, "f.h.dragon.DragonTester", null, "java/lang/Object", new String[]{"java.lang.Cloneable"});
		cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "LESS", "I", null, new Integer(-1)).visitEnd();
		cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "EQUAL", "I", null, new Integer(0)).visitEnd();
		cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "GREATER", "I", null, new Integer(1)).visitEnd();
		cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "test", "(Ljava/lang/Object;)I", null, null).visitEnd();
		cw.visitEnd();
		outln(cw);
		byte[] b = cw.toByteArray();
		outln(new String(b));
//		ParseClass pc = new ParseClass();
//		ClassReader cr = new ClassReader(b);
//		cr.accept(pc, 0);
		ClassLoaderTester clt = new ClassLoaderTester();
		Class c = clt.defineClass("f.h.dragon.DragonTester", b);
//		outln(c.getCanonicalName());
		
	}
	
	/**
	 * 生成类测试1
	 */
	public static void generatorClass2(){
		ClassWriter cw = new ClassWriter(0);
		cw.visit(V1_5, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, "DragonTester", null, "java/lang/Object", null);
		cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "LESS", "I", null, new Integer(-1)).visitEnd();
		cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "EQUAL", "I", null, new Integer(0)).visitEnd();
		cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "GREATER", "I", null, new Integer(1)).visitEnd();
		cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "test", "(Ljava/lang/Object;)I", null, null).visitEnd();
		cw.visitEnd();
		outln(cw);
		byte[] b = cw.toByteArray();
		outln(new String(b));
		ClassLoaderTester clt = new ClassLoaderTester();
		Class c = clt.defineClass("f.h.dragon.DragonTester", b);
//		outln(c.getCanonicalName());
	}
	
	/**
	 * 生成类测试1
	 */
	public static void generatorClass1(){
		ClassWriter cw = new ClassWriter(0);
		cw.visit(V1_5, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, "f.h.dragon.DragonTester", null, "java/lang/Object", new String[]{"java.lang.Cloneable"});
		cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "LESS", "I", null, new Integer(-1)).visitEnd();
		cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "EQUAL", "I", null, new Integer(0)).visitEnd();
		cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "GREATER", "I", null, new Integer(1)).visitEnd();
		cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "test", "(Ljava/lang/Object;)I", null, null).visitEnd();
		cw.visitEnd();
		outln(cw);
		byte[] b = cw.toByteArray();
		outln(new String(b));
		ParseClass pc = new ParseClass();
		ClassReader cr = new ClassReader(b);
		cr.accept(pc, 0);
	}
	
}

/**
 * GenerateClass : generate class by ClassWriter
 *
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2012/04/01
 * @since 1.0
 */
class GenerateClass extends ClassWriter {

	//Constructor
	/**
	 * 构建指定api的解析类对象
	 * 
	 * @param see {@link Opcodes#ASM4}
	 */
	public GenerateClass() {
		super(ASM4);
	}
}
